/*
 *  CENodeScissored.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_NodeScissored_h__
#define __CE_NodeScissored_h__

#include "cocos2d.h"

namespace cocosext {

/////////////////////////////////////////////////////
//
/** CENodeScissored 
* CENodeScissored is a node, which clipp its's content
* !!! It is not rotatable node
*/
/////////////////////////////////////////////////////

template <typename T>
class CENodeScissored : public T
{

public:
	static CENodeScissored* node()
	{
		CENodeScissored *pRet = new CENodeScissored();
		CCAssert(pRet != NULL, NULL);
		if (pRet->init())
		{
			pRet->autorelease();
			return pRet;
		}
		else
		{
			CC_SAFE_DELETE(pRet)
			return NULL;
		}
	}

	virtual bool init() 
	{ 
		if (!T::init() )
			return false;

		return true;
	}

public:
// Cocos2dx
    virtual void visit()
	{
    	cocos2d::CCAffineTransform transform = this->nodeToWorldTransform();
    	cocos2d::CCRect viewport = cocos2d::CCEGLView::sharedOpenGLView().getViewPort();
    	float screenScaleFactor = cocos2d::CCEGLView::sharedOpenGLView().getScreenScaleFactor();
    	cocos2d::CCRect rect(transform.tx*screenScaleFactor + viewport.origin.x,
    						 transform.ty*screenScaleFactor + viewport.origin.y,
							 this->getContentSizeInPixels().width*screenScaleFactor,
							 this->getContentSizeInPixels().height*screenScaleFactor);
		
		glEnable(GL_SCISSOR_TEST);
		
		glScissor((GLint)rect.origin.x, (GLint)rect.origin.y, (GLsizei)rect.size.width, (GLsizei)rect.size.height);
		cocos2d::CCNode::visit();
		
		glDisable(GL_SCISSOR_TEST);
	}

	virtual void setRotation(float fRotation)
	{
		CCAssert(false, "CENodeScissored::setRotation: CENodeScissored node can't be rotated!");
	}
};


}; // end namespace cocosext

#endif // __CE_NodeScissored_h__
